#include "model_list.h"

ModelList::ModelList(QWidget* parent) : QListWidget(parent), _context(nullptr)
{
    //connect(this, &QListWidget::itemClicked, this, &ModelList::OnItemClicked);
    connect(this, &QListWidget::itemPressed, this, &ModelList::OnItemClicked);
    //connect(this, &QListWidget::itemEntered, this, &ModelList::OnItemClicked);
}

ModelList::~ModelList()
{
}

void ModelList::SetupContext(const Handle(AIS_InteractiveContext)& context)
{
    _context = context;
}

void ModelList::RecordModel(const Handle(AIS_InteractiveObject)& ais, const QString& name)
{
    if (_context && ais)
    {
        _context->Display(ais, true);

        QListWidgetItem* item = new QListWidgetItem(this);
        item->setData(Qt::UserRole, qulonglong(ais.get()));
        item->setText(name);
        this->addItem(item);
        this->setCurrentItem(item);
    }
}

void ModelList::Clear()
{
    if (_context)
    {
        _context->RemoveAll(false);
    }
    clear();
}

void ModelList::OnItemClicked(QListWidgetItem* item)
{
    if (!item)
    {
        return;
    }
    QVariant raw_data = item->data(Qt::UserRole);
    void* ptr = (void*)raw_data.toULongLong();

    Handle(AIS_InteractiveObject) ais;
    ais.reset((AIS_InteractiveObject*)ptr);

    if (ais && _context)
    {
        if (_context->IsDisplayed(ais))
        {
            _context->Erase(ais, true);
            item->setForeground(QBrush(Qt::gray)); // 隐藏置灰
        }
        else
        {
            _context->Display(ais, true);
            //_context->Activate(ViewHelper::SM_FACE);
            //_context->Activate(ViewHelper::SM_WIRE);
            item->setForeground(QBrush(Qt::black)); // 显示
        }
    }
}
